from django.db import models
#from User.models import User

# Create your models here.


'''
    Department:部门表
        deptName:科室
'''
class Department(models.Model):
    deptName = models.CharField(max_length=20)

    class Meta():
        db_table = 'Department'

'''
    Position：职务表
        posName：职务名
'''
class Position(models.Model):
    posName = models.CharField(max_length=20)

    class Meta():
        db_table = 'Position'

'''
    Staff：员工表
        job：职称
        positions：职务id(一对多)
        name：姓名
        gender：性别
        departments：部门id（一对多）
        isDelete：是否删除
'''
class Staff(models.Model):
    job = models.CharField(max_length = 20)
    positions = models.ForeignKey(Position, on_delete = models.CASCADE)
    name = models.CharField(max_length = 20)
    gender = models.CharField(max_length = 2)
    departments = models.ForeignKey(Department, on_delete = models.CASCADE)
    isDelete = models.BooleanField(default = False)
    class Meta():
        db_table = 'Staff'
'''-------------------------------------------------------------------------'''


'''
    模型：Role：权限表
         roleName：角色名
'''
class Role(models.Model):
    roleName=models.CharField(max_length=2)
    class Meta:
        db_table='Role'
'''
    模型：User:用户表
         username：用户名
         password：密码
         email：邮箱
         isDelete：是否删除
         staffs：员工id
         roles：角色id
'''
class User(models.Model):
    username=models.CharField(max_length=20)
    password=models.CharField(max_length=64)
    email=models.EmailField()
    isDelete=models.BooleanField(default=False)
    staffs=models.ForeignKey(Staff, on_delete=models.CASCADE)
    roles=models.ForeignKey(Role,on_delete=models.CASCADE)
    class Meta:
        db_table='User'
'''----------------------------------------------------------------------'''


'''
    Filetype:文件类型表
        fTypename:文件类型
'''
class Filetype(models.Model):
    fTypename = models.CharField(max_length=20)
    class Meta():
        db_table = 'Filetype'
'''
    File:文件表
        fileName:文件名
        filePath:文件路径
        loadDate:上传日期
        isShare:是否共享
        filetypes:文件分类id
        users:用户id
'''

class FileManager(models.Manager):
    def createfile(self, name, path, share, type, user):
        file = self.model()
        file.fileName = name
        file.filePath = path
        file.isShare = share
        file.filetypes = type
        file.users = user
        file.save()
    def listfile(self):
        return super().all().filter(isDelete=False).filter(isShare=True)
    def searchfile(self, name):
        return super().filter(fileName__contains = name)
    def searchid(self, id):
        return  super().filter(pk = id)


class File(models.Model):
    fileName = models.CharField(max_length=50)
    filePath = models.FileField(upload_to = 'modfile/', max_length = 100)
    loadDate = models.DateField(auto_now=True)
    changeDate = models.DateField(auto_now=True)
    isShare = models.BooleanField()
    isDelete = models.BooleanField(default=False)
    filetypes = models.ForeignKey('Filetype', on_delete=models.CASCADE)
    users = models.ForeignKey(to=User, on_delete=models.CASCADE)
    file = FileManager()
    class Meta():
        db_table = 'File'
